package org.wendu.wdoa.security.home;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wendu.wdoa.security.home.dto.MenuDto;
import org.wendu.wdoa.security.home.model.Fun;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class HomeServiceImpl implements HomeService{

    @Autowired
    private HomeDao homeDao;

    @Override
    public List<MenuDto> getMenuList(String userId) {
        List<Fun> funList = homeDao.findFunsByUserId(userId);

        List<MenuDto> mainList = new ArrayList<>();//主菜单集合
        MenuDto currentMain = null;//当前主菜单

        for (Fun fun:funList) {
            if (currentMain == null || !currentMain.getMenuId().equals(fun.getP_id())){
                currentMain = new MenuDto();
                currentMain.setMenuId(fun.getP_id());
                currentMain.setMenuName(fun.getP_name());
                currentMain.setSubMenuList(new ArrayList<MenuDto>());

                mainList.add(currentMain);
            }
            MenuDto sub = new MenuDto();
            sub.setMenuId(fun.getF_id());
            sub.setMenuName(fun.getF_name());
            sub.setMenuUrl(fun.getF_url());

            currentMain.getSubMenuList().add(sub);
        }

        return mainList;
    }
}
